package com.stella.ieproductivitybackend.service.impl;

import com.stella.ieproductivitybackend.entity.User;
import com.stella.ieproductivitybackend.mapper.LoginMapper;
import com.stella.ieproductivitybackend.service.LoginService;
import com.stella.ieproductivitybackend.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录页服务层
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public Map<String, Object> getUser(User user) {
        Map<String, Object> map = new HashMap<>();

        try{
//            User userDB = loginMapper.getUser(user);
            User userDB = null;
            if ("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())) {
                userDB = new User();
                userDB.setUsername("admin");
                userDB.setPassword("123456");
            }

            Map<String, String> payload = new HashMap<>();
            payload.put("username", userDB.getUsername());
            payload.put("password", userDB.getPassword());
            //生成 JWT 令牌
            String token = JWTUtils.getToken(payload);
            map.put("state", true);
            map.put("msg", "登录成功！");
            map.put("token", token);  // 响应 token
        } catch (Exception e) {
            map.put("state", false);
            map.put("msg", "用户名或密码错误！");
        }

        return map;
    }
}
